<?php
/**
 * Save form valuers in database
 *
 * Structure declaration example :
 *
 * <onSuccess>
 *      <action type="SendMail">
 *          <param name="mailto">{email}</param>
 *      </action>
 * </onSuccess>
 */
class m_form_plugins_success_SendMail extends m_form_extends_SuccessAction
{
    public function execute()
    {
        $mail = f_mail_Message::getInstance();
        
        $tpl = $this->getParameter('htmlMessageTemplate');

        $html = $this->getParameter('htmlMessage');
        
        if(!is_null($tpl))
        {
            $html = $this->getTemplate();
        }
        
        $mail   ->setFrom($this->getParameter('senderMail'), $this->getParameter('senderName'))
                ->setTo($this->getParameter('receiverMail'))
                ->setSubject($this->getParameter('subject'))
                ->setBodyHtml($html);

        if(!is_null($this->getParameter('textMessage')))
        {
            $mail->setBodyText($this->getParameter('textMessage'));
        }

        $mail->send();

        return true;
    }

    private function getTemplate()
    {
        $tplEngine = f_template_Template::getInstance();

        $tplEngine->setModuleTemplate('form', 'mails/'.$this->getParameter('htmlMessageTemplate').'.html');

        foreach($this->getFormValues() as $k => $v)
        {
            $tplEngine->assign($k, $v);
        }

        $tplEngine->assign('formValues', $this->getFormValues());

        $tplEngine->assign('website', $this->getContextWebsite());
        $tplEngine->assign('page', $this->getContextPage());

        return $tplEngine->render();
    }
}